const jwt = require('jsonwebtoken');
const redisUtil = require('../libs/redis-util');

const prefix = ['/zlz_api/appAuthorize','/zlz_api/kingdeeApi','/zlz_api/kingdeeOldApi'];

const authentication = async (ctx, next) => {
  if (prefix.includes(ctx.request.path)) {
    ctx.response.set('Cache-Control', 'max-age=600, public');
    await next();
    return;
  }
  // const { authorization } = ctx.request.headers;
  // if (!authorization) {
  //   ctx.body = Res.error(Mes.INVALID_AUTHORIZATION);
  //   return;
  // }

  // ctx.request.jwt = authorization;
  // ctx.request.jwtToken = authorization;

  ctx.response.set('Pragma', 'no-cache');
  ctx.response.set('Cache-Control', 'no-store');
  ctx.response.set('X-Content-Type-Options', 'nosniff');
  ctx.response.set('Strict-Transport-Security', 'max-age=8995000; includeSubdomains');
  ctx.response.set('Content-Type', 'application/json; charset=utf-8');
  // try {
  //   const payload = jwt.verify(authorization, config.jwtKey.tokenKey);
  //   ctx.request.accountId = payload.accountId;
  //   ctx.request.mobile = payload.mobile;
  //   ctx.request.userid = payload.userid;
  //   ctx.request.dept_id = payload.dept_id;
  //   ctx.request.dept_number = payload.dept_number;
  //   ctx.request.emp_id = payload.emp_id;
  //   ctx.request.emp_number = payload.emp_number;
  //   const redisJwtToken = await redisUtil.get(`jwt:${payload.accountId}:userid_${payload.userid}`);
  //   if (redisJwtToken && redisJwtToken === authorization) {
      await next();
  //   } else {
  //     console.error('授权认证失败, 请重新进入');
  //     ctx.body = Res.error(Mes.INVALID_AUTHORIZATION);
  //   }
  // } catch (error) {
  //   console.error('jwtToken无效 >>> %s', error.message);
  //   ctx.body = Res.error(Mes.INVALID_AUTHORIZATION);
  // }
};

module.exports = authentication;
